

Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports Csla
Imports Csla.Data

<Serializable()> _
Public Class rw_playerChildList
    Inherits Csla.BusinessListBase(Of rw_playerChildList, rw_playerChild)
    Private _teamId As Integer

    Public Property teamId() As Integer
        Get
            Return _teamId
        End Get
        Set(ByVal value As Integer)
            If value <> _teamId Then
                _teamId = value
            End If
        End Set
    End Property
#Region " BindingList Overrides "

    Protected Overrides Function AddNewCore() As Object
        Dim item As rw_playerChild = rw_playerChild.NewPlayerChild()
        Me.Add(item)
        Return item
    End Function

#End Region ' BindingList Overrides

#Region " Factory Methods "

    Public Shared Function NewPlayerChildList() As rw_playerChildList
        Return New rw_playerChildList()
    End Function

    Friend Shared Function GetPlayerChildList(ByVal dr As SafeDataReader) As rw_playerChildList
        Return New rw_playerChildList(dr)
    End Function

    Public Shared Function getPlayers(ByVal teamId As Integer) As rw_playerChildList
        Return DataPortal.Fetch(Of rw_playerChildList)(New Criteria(teamId))
    End Function
    Private Sub New()
        MarkAsChild()
    End Sub

    Private Sub New(ByVal dr As SafeDataReader)
        MarkAsChild()
        Fetch(dr)
    End Sub

#End Region ' Factory Methods

#Region " Data Access "
    <Serializable()> _
    Private Class Criteria
        Public _teamId As Integer
        ' no criteria
        Public Sub New(ByVal myTeamId)
            _teamId = myTeamId
        End Sub
    End Class

    Private Sub Fetch(ByVal dr As SafeDataReader)
        RaiseListChangedEvents = False

        While dr.Read()
            Me.Add(rw_playerChild.Getrw_playerChild(dr))
        End While

        RaiseListChangedEvents = True
    End Sub

    Private Sub Fetch(ByVal criteria As Criteria)
        Me.RaiseListChangedEvents = False

        Dim myConnectionObj As New connection
        Dim myConnection As SqlClient.SqlConnection
        myConnection = myConnectionObj.ReadMySettings()

        Using cn As SqlConnection = New SqlConnection(myConnection.ConnectionString.ToString)
            cn.Open()
            Using cm As SqlCommand = cn.CreateCommand
                cm.CommandType = CommandType.StoredProcedure
                cm.CommandText = "sp_getTeamList"
                cm.Parameters.AddWithValue("@teamId", criteria._teamId)
                Using dr As New SafeDataReader(cm.ExecuteReader)
                    With dr
                        While .Read()
                            Me.Add(rw_playerChild.Getrw_playerChild(dr))
                        End While
                    End With
                End Using
            End Using
        End Using
        Me.RaiseListChangedEvents = True
    End Sub

    Friend Sub Update(ByVal cn As SqlConnection, ByVal parent As rw_teamRoot)
        RaiseListChangedEvents = False

        ' loop through each deleted child object
        For Each deletedChild As rw_playerChild In DeletedList
            deletedChild.DeleteSelf(cn)
        Next
        DeletedList.Clear()

        ' loop through each non-deleted child object
        For Each child As rw_playerChild In Me
            If child.IsNew Then
                child.Insert(cn, parent)
            Else
                child.Update(cn, parent)
            End If
        Next

        RaiseListChangedEvents = True
    End Sub

#End Region ' Data Access

End Class
